package com.kyc.kycxkplaygame.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.kyc.kycxkplaygame.R;
import com.kyc.kycxkplaygame.dbutils.GetPropLevelUtils;
import com.kyc.kycxkplaygame.dbutils.IntImp.AlasbusyIApi;
import com.kyc.kycxkplaygame.dbutils.inter.HttpQuestResultListener;
import com.kyc.kycxkplaygame.entity.PropsCode;
import com.kyc.kycxkplaygame.greendao.LevelPassBean;
import com.kyc.kycxkplaygame.greendao.manager.LevelInfoManager;
import com.kyc.kycxkplaygame.util.ConstactUtil;
import com.kyc.kycxkplaygame.util.SharedPreferencesUtils;
import com.kyc.kycxkplaygame.util.TimeUtil;
import com.zhy.android.percent.support.PercentRelativeLayout;
import es.dmoral.toasty.Toasty;

import java.util.List;

public class StartPlayActivity extends Activity implements View.OnClickListener, HttpQuestResultListener {

    private String level_num;
    private ImageView start_play_stars;
    private String tili_num;
    private ImageView start_btn_click;
    private PercentRelativeLayout tishi_lin;
    private PercentRelativeLayout zhadan_lin;
    private PercentRelativeLayout time_lin;
    private ImageView tishi_success;
    private ImageView zhadan_success;
    private ImageView time_success;
    private TextView tishi_money;
    private TextView zhadan_money;
    private TextView time_money;
    private AlasbusyIApi alasbusyIApi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setFinishOnTouchOutside(false);
        setContentView(R.layout.activity_start_play);
        Window window = getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        layoutParams.alpha = 1f;
        window.setAttributes(layoutParams);

        level_num = getIntent().getStringExtra("level_num");
        tili_num = getIntent().getStringExtra("tili_num");
        initView();

        initData();

        initListener();
    }

    private void initView() {
        start_play_stars = findViewById(R.id.start_play_stars);
        start_btn_click = findViewById(R.id.start_btn_click);

        tishi_lin = findViewById(R.id.tishi_lin);
        zhadan_lin = findViewById(R.id.zhadan_lin);
        time_lin = findViewById(R.id.time_lin);

        tishi_money = findViewById(R.id.tishi_money);
        zhadan_money = findViewById(R.id.zhadan_money);
        time_money = findViewById(R.id.time_money);

        tishi_success = findViewById(R.id.tishi_success);
        zhadan_success = findViewById(R.id.zhadan_success);
        time_success = findViewById(R.id.time_success);
    }


    private void initData() {
        if (Integer.parseInt(tili_num) <= 0) {
            start_btn_click.setImageResource(R.drawable.start_play_btn_xiyou);
        }

        List<LevelPassBean> levelPassBeen = LevelInfoManager.getInstance().getUserInfo();
        if (levelPassBeen.size() > 0) {
            for (LevelPassBean bean : levelPassBeen) {
                if (Integer.parseInt(bean.getLevel()) == Integer.parseInt(level_num)) {
                    if (bean.getStarCount() == 1) {
                        start_play_stars.setImageResource(R.drawable.one_stars);
                    } else if (bean.getStarCount() == 2) {
                        start_play_stars.setImageResource(R.drawable.two_stars);
                    } else if (bean.getStarCount() == 3) {
                        start_play_stars.setImageResource(R.drawable.three_stars);
                    }
                }
            }
        } else {
            start_play_stars.setBackgroundResource(R.drawable.three_stars_nop);
        }
        /*if (*//*Integer.parseInt(level_num) > 1*//*map_istrue) {
            HashMap<String, PassStatusBean> pass_status = SharedPreferencesUtils.getHashMapData(StartPlayActivity.this, "pass_status", PassStatusBean.class);
            for (Map.Entry<String, PassStatusBean> set : pass_status.entrySet()) {
                PassStatusBean setValue = set.getValue();
                if (Integer.parseInt(setValue.getLevel()) == Integer.parseInt(level_num)) {
                    if (setValue.getFraction().equals("1")) {
                        start_play_stars.setImageResource(R.drawable.one_stars);
                    } else if (setValue.getFraction().equals("2")) {
                        start_play_stars.setImageResource(R.drawable.two_stars);
                    } else if (setValue.getFraction().equals("3")) {
                        start_play_stars.setImageResource(R.drawable.three_stars);
                    }
                }
            }
        } else {
            start_play_stars.setBackgroundResource(R.drawable.three_stars_nop);
        }*/
    }

    private void initListener() {
        findViewById(R.id.start_close_img).setOnClickListener(this);
        start_btn_click.setOnClickListener(this);
        tishi_lin.setOnClickListener(this);
        zhadan_lin.setOnClickListener(this);
        time_lin.setOnClickListener(this);

        alasbusyIApi = AlasbusyIApi.getInstance(this);
        alasbusyIApi.setHttpQuestResultListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.start_close_img:
                onBackPressed();
                break;
            case R.id.start_btn_click:
                if (Integer.parseInt(tili_num) > 0) {
                    //SharedPreferencesUtils.getParam(StartPlayActivity.this,"tili_num",String.valueOf(Integer.parseInt(tili_num) - 1));
                    SharedPreferencesUtils.setParam(StartPlayActivity.this, "oldtime", TimeUtil.getNowYMDHMSTime());
                    Intent intent = new Intent(StartPlayActivity.this, GameActivity.class);
                    intent.putExtra("level_num", level_num);
                    intent.putExtra("status", "tili");
                    startActivity(intent);
                    onBackPressed();
                } else {
                    int xiyou = Integer.parseInt(GetPropLevelUtils.getInstance().getPropNum("西柚"));//(int) SharedPreferencesUtils.getParam(StartPlayActivity.this, "xiyou", 0);
                    if (xiyou > 0) {
                        SharedPreferencesUtils.setParam(StartPlayActivity.this, "oldtime", TimeUtil.getNowYMDHMSTime());
                        Intent intent = new Intent(StartPlayActivity.this, GameActivity.class);
                        intent.putExtra("level_num", level_num);
                        intent.putExtra("status", "xiyou");
                        startActivity(intent);
                        onBackPressed();
                    } else {
                        Intent intent = new Intent(StartPlayActivity.this, PropsBuyActivity.class);
                        intent.putExtra("daoju", "xiyou");
                        startActivity(intent);
                        onBackPressed();
                    }
                }
                break;
            case R.id.tishi_lin:
                int propNum = Integer.parseInt(GetPropLevelUtils.getInstance().getPropNum("葡萄"));
                if (propNum > 40) {
                    int remaining = propNum - 40;
                    alasbusyIApi.insertProps(GetPropLevelUtils.getInstance().getUserName(), "葡萄", remaining);
                    GetPropLevelUtils.getInstance().updatePropNum("葡萄", remaining);
                    alasbusyIApi.insertProps(GetPropLevelUtils.getInstance().getUserName(), "提示", 1);
                    GetPropLevelUtils.getInstance().updateNoFriutPropNum("提示", 1);
                    tishi_money.setVisibility(View.GONE);
                    tishi_success.setVisibility(View.VISIBLE);
                    tishi_lin.setEnabled(false);
                } else {
                    Toasty.warning(StartPlayActivity.this, "葡萄币不够,无法购买.", Toast.LENGTH_SHORT).show();
                    return;
                }
                //sendGrapeBroad();
                break;
            case R.id.zhadan_lin:
                int propNumzha = Integer.parseInt(GetPropLevelUtils.getInstance().getPropNum("葡萄"));
                if (propNumzha > 100) {
                    int remaining = propNumzha - 40;
                    alasbusyIApi.insertProps(GetPropLevelUtils.getInstance().getUserName(), "葡萄", remaining);
                    GetPropLevelUtils.getInstance().updatePropNum("葡萄", remaining);
                    alasbusyIApi.insertProps(GetPropLevelUtils.getInstance().getUserName(), "炸弹", 1);
                    GetPropLevelUtils.getInstance().updateNoFriutPropNum("炸弹", 1);
                    zhadan_money.setVisibility(View.GONE);
                    zhadan_success.setVisibility(View.VISIBLE);
                    zhadan_lin.setEnabled(false);
                } else {
                    Toasty.warning(StartPlayActivity.this, "葡萄币不够,无法购买.", Toast.LENGTH_SHORT).show();
                    return;
                }
                //sendGrapeBroad();
                break;
            case R.id.time_lin:
                int propNumtime = Integer.parseInt(GetPropLevelUtils.getInstance().getPropNum("葡萄"));
                if (propNumtime > 60) {
                    int remaining = propNumtime - 40;
                    alasbusyIApi.insertProps(GetPropLevelUtils.getInstance().getUserName(), "葡萄", remaining);
                    GetPropLevelUtils.getInstance().updatePropNum("葡萄", remaining);
                    alasbusyIApi.insertProps(GetPropLevelUtils.getInstance().getUserName(), "时间", 1);
                    GetPropLevelUtils.getInstance().updateNoFriutPropNum("时间", 1);
                    time_money.setVisibility(View.GONE);
                    time_success.setVisibility(View.VISIBLE);
                    time_lin.setEnabled(false);
                } else {
                    Toasty.warning(StartPlayActivity.this, "葡萄币不够,无法购买.", Toast.LENGTH_SHORT).show();
                    return;
                }
                break;
        }
    }

    public void sendGrapeBroad() {
        new Thread(){
            @Override
            public void run() {
                Intent intent = new Intent(ConstactUtil.UpdateGrapeAction);
                sendBroadcast(intent);
            }
        }.start();

    }

    @Override
    public void onBackPressed() {
        StartPlayActivity.this.finish();
    }

    @Override
    public void onError(Object e) {

    }

    @Override
    public void onSuccess(Object object) {
        if (object instanceof PropsCode) {
            PropsCode propsCode = (PropsCode) object;
            if (propsCode.getCode() == 200) {
                Log.i("KYCC", propsCode.getMessage());
                sendGrapeBroad();
            }
        }
    }

    @Override
    public void onQuestErroCode(Object object) {
        if (object instanceof PropsCode) {
            PropsCode propsCode = (PropsCode) object;
        }
    }
}
